package com.kool.kadmin.system.controller;

import com.kool.kadmin.common.bean.ObjectResult;
import com.kool.kadmin.common.bean.PageListResult;
import com.kool.kadmin.common.bean.Result;
import com.kool.kadmin.common.cache.TokenVo;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.api.vo.UserVo;
import com.kool.kadmin.system.request.*;
import com.kool.kadmin.system.service.UserService;
import com.kool.kadmin.system.vo.FileVo;
import com.kool.kadmin.system.vo.PreloginVo;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author luyu
 */
@RestController
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 预登录
     *
     * @return
     */
    @RequestMapping("/user/prelogin")
    public ObjectResult<PreloginVo> prelogin(@Valid @RequestBody PreloginReq request) {
        return userService.prelogin(request);
    }

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/user/login")
    public ObjectResult<TokenVo> login(@Valid @RequestBody LoginReq request) {
        return userService.login(request);
    }

    /**
     * 增加用户
     *
     * @return
     */
    @RequestMapping("/user/add")
    public Result add(@Valid @RequestBody AddUserReq request) {
        return userService.addUser(request);
    }

    /**
     * 修改用户
     *
     * @return
     */
    @RequestMapping("/user/modify")
    public Result modify(@Valid @RequestBody ModifyUserReq request) {
        return userService.modifyUser(request);
    }

    /**
     * 修改个人信息
     *
     * @return
     */
    @RequestMapping("/user/updateMyInfo")
    public Result updateMyInfo(@Valid @RequestBody UpdateMyInfoReq request) {
        return userService.updateMyInfo(request);
    }
    /**
     * 关闭用户
     *
     * @return
     */
    @RequestMapping("/user/close")
    public Result close(@Valid @RequestBody CloseUserReq request) {
        return userService.closeUser(request);
    }

    /**
     * 分页查询用户
     *
     * @return
     */
    @RequestMapping("/user/get_user_page")
    public PageListResult<UserVo> getUserPage(@Valid @RequestBody GetUserPageReq request) {
        return userService.getUserPage(request);
    }

    /**
     * 查询用户详情
     *
     * @return
     */
    @RequestMapping("/user/get_user")
    public ObjectResult<UserVo> getUser(@Valid @RequestBody GetUserReq request) {
        return userService.getUser(request);
    }

    /**
     * 重置密码为默认值
     *
     * @param request
     * @return
     */
    @RequestMapping("/user/reset_pwd")
    public Result resetPwd(@Valid @RequestBody ResetPwdReq request) {
        return userService.resetPwd(request);
    }

    /**
     * 修改密码
     *
     * @param request
     * @return
     */
    @RequestMapping("/user/modify_pwd")
    public Result modifyPwd(@Valid @RequestBody ModifyPwdReq request) {
        return userService.modifyPwd(request);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/user/logout")
    public Result logout(@Valid @RequestBody LogoutReq request) {
        return userService.logout(request);
    }

    /**
     * 上传头像
     *
     * @param uploadFile
     * @return
     */
    @RequestMapping(value = "/user/uploadFace")
    public JSONObject uploadFace(@RequestParam("file") MultipartFile uploadFile) {

        JSONObject joOut = new JSONObject();
        FileVo fileVo = userService.uploadFace(uploadFile);
        if (fileVo != null) {
            joOut.put("fileName", fileVo.getFileName());
            joOut.put("fileUrl", fileVo.getFileUrl());
        }
        return joOut;
    }

    /**
     * 验证token
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/user/validateToken")
    public ObjectResult<Object> validateToken(@Valid @RequestBody ValidateTokenReq request) {
        boolean flag = userService.validateToken(request.getUserId(), request.getToken());
        return ResultUtil.success(flag);
    }
}
